/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-20
 * Time: 0:15
 */

//动态规划一>最小路径和
public class Demo {
    /**
     时空复杂度都是O(M*N)
     */
    public int minPathSum(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] dp = new int[m+1][n+1];

        //初始化
        for(int i = 0; i < n+1; i++) dp[0][i] = Integer.MAX_VALUE;
        for(int j = 0; j < m+1; j++) dp[j][0] = Integer.MAX_VALUE;
        dp[1][0] = dp[0][1] = 0;

        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++)
                dp[i][j] = Math.min(dp[i][j-1],dp[i-1][j]) + grid[i-1][j-1];

        return dp[m][n];
    }
}
